Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
"dynamic" parameters in a strongly-typed language?

I've been learning to code and recently finished a server-client project in javascript. In that server (Node.js) I wrote myself a "responder" function that I called dozens of times throughout the program:

function sendResponse(req, res, err, response, redirect) {
    if (err) {
        res.status(err.status)
        return res.json(err.message || err.error)
    }
    if (response) {
        res.status(response.status)
        return res.json(response.response || response.message)
    }
    if (redirect) {
        return res.redirect(redirect)
    }
    res.status(500)
    return res.json({message: err.err || "Unknown server error", user: req.userObj.email || null})
}

I'm now trying to write a similar API-serving backend in Go. While of course you can pass nil to functions, what I can't figure out is how to allow any of those parameters to be "dynamic" - perhaps that is the point of a statically typed language? In javascript, the response parameter could be an object with string keys & values, or it could have nested objects with additional data to send back to the client. In Go, I could take a map as a parameter, but I'd have to define it in advance as a string:string map. If I wanted to nest a map, I'd also have to define the mappings of the nested map.

This is the closest I've gotten in Go:

func responder(w http.ResponseWriter, m map[string]string, s int) {
    w.WriteHeader(s)
    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    if m != nil {
        r := createJson(m)
        w.Write(r)
        return
    }
    w.Write([]byte("Good job"))
    return
}

func createJson(m map[string]string) ([]byte) {
    j, err := json.Marshal(m)
    if err != nil {
        fmt.Println(err)
        return nil
    }
    return j
}

But I'm starting to get the feeling I am just thinking about how to do things in statically typed languages incorrectly.

Edit: Is this a job for pointers? Could I use a pointer to a map that may or may not contain nested maps?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you can make a struct with a type field inside the struct and act according to the type using switch case.

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use interface{} here as shown below

func responder(w http.ResponseWriter, m interface{}, s int) {
    w.WriteHeader(s)
    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    if m != nil {
        r := createJson(m)
        w.Write(r)
        return
    }
    w.Write([]byte("Good job"))
    return
}

func createJson(m interface{}) ([]byte) {
    j, err := json.Marshal(m)
    if err != nil {
        fmt.Println(err)
        return nil
    }
    return j
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda